package com.steven.treatment_nurse.activity;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.steven.treatment_nurse.R;
import com.steven.treatment_nurse.base.BaseActivity;
import com.steven.treatment_nurse.entity.HSBedEntity;
import com.steven.treatment_nurse.fragment.BloodOFragment;
import com.steven.treatment_nurse.fragment.BloodRressureFragment;
import com.steven.treatment_nurse.fragment.BloodSugarFragment;
import com.steven.treatment_nurse.fragment.BodyTFragment;
import com.steven.treatment_nurse.fragment.HeartRateFragment;
import com.steven.treatment_nurse.fragment.LocationFragment;
import com.steven.treatment_nurse.fragment.MonitFragment;
import com.steven.treatment_nurse.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

public class HSBedDetailActivity extends BaseActivity {
    @BindView(R.id.tv_week)
    TextView tvWeek;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_time)
    TextView tvTime;
    private TimeReceive timeReceive;
    @BindView(R.id.iv_back)
    ImageView iv_back;

    @BindView(R.id.vp)
    ViewPager viewPager;

    @BindView(R.id.tv_name)
    TextView tv_name;//姓名 性别 年龄

    @BindView(R.id.tv_bedName)
    TextView tv_bedName;//床位号

    @BindView(R.id.tv_ks)
    TextView tv_ks;//科室

    @BindView(R.id.tv_address)
    TextView tv_address;//地址

    @BindView(R.id.tv_zyh)
    TextView tv_zyh;//住院号

    @BindView(R.id.tv_zzys)
    TextView tv_zzys;//主治医师

    @BindView(R.id.tv_zzhs)
    TextView tv_zzhs;//责任护士

    @BindView(R.id.tv_t)
    TextView tv_t;//入院时间

    @BindView(R.id.tv_zd)
    TextView tv_zd;//疾病

    private List<Fragment> fragments = new ArrayList<>();
    private HSBedEntity hsBedEntity;
    private String batchId;

    @OnClick({R.id.tv_bloodP, R.id.tv_bloodH
            , R.id.tv_bloodS, R.id.tv_bloodO
            , R.id.tv_bodyT, R.id.tv_monit
            , R.id.tv_location
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_bloodP://血压
                viewPager.setCurrentItem(0);
                break;
            case R.id.tv_bloodH://心率
                viewPager.setCurrentItem(1);
                break;

            case R.id.tv_bloodS://血糖
                viewPager.setCurrentItem(2);
                break;
            case R.id.tv_bloodO://血氧
                viewPager.setCurrentItem(3);
                break;

            case R.id.tv_bodyT://体温
                viewPager.setCurrentItem(4);
                break;
            case R.id.tv_monit://监控
                viewPager.setCurrentItem(5);
                break;

            case R.id.tv_location://定位
                viewPager.setCurrentItem(6);
                break;
        }
    }


    @Override
    public int intiLayout() {
        return R.layout.activity_hs_bed_detail;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeReceive != null) {
            unregisterReceiver(timeReceive);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initView() {
        hsBedEntity = (HSBedEntity) getIntent().getSerializableExtra("hs");
        batchId=hsBedEntity.getId()+"";
        tv_name.setText(hsBedEntity.getName() + "  " + hsBedEntity.getSex() + "  " + hsBedEntity.getAge());
        tv_bedName.setText("病床号: " + hsBedEntity.getHsBed());
        tv_ks.setText("电话: " + hsBedEntity.getPhone());
        tv_address.setText("住址: ");
        tv_zyh.setText("住院号: ");
        tv_zzys.setText("主治医师: ");
        tv_zzhs.setText("责任护士: ");
        tv_t.setText("入院时间: " + hsBedEntity.getAdmissionTime());
        tv_zd.setText("入院诊断: " + hsBedEntity.getCaseHS());
        timeReceive = new TimeReceive();
        tvWeek.setText(Objects.requireNonNull(TimeUtil.getTime()).get(2));
        tvDate.setText(TimeUtil.getTime().get(0));
        tvTime.setText(TimeUtil.getTime().get(1));
        IntentFilter s_intentFilter;
        s_intentFilter = new IntentFilter();
        s_intentFilter.addAction(Intent.ACTION_TIME_TICK);
        s_intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
        registerReceiver(timeReceive, s_intentFilter);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initFragment();
    }


    //初始化fragment
    private void initFragment() {
        fragments.add(new BloodRressureFragment(batchId));
        fragments.add(new HeartRateFragment(batchId));
        fragments.add(new BloodSugarFragment(batchId));
        fragments.add(new BloodOFragment(batchId));
        fragments.add(new BodyTFragment(batchId));
        fragments.add(new MonitFragment(hsBedEntity));
        fragments.add(new LocationFragment(batchId));

        viewPager.setAdapter(new PagerAdapter(this.getSupportFragmentManager()));
        viewPager.setOffscreenPageLimit(fragments.size() - 1);
        viewPager.setCurrentItem(0);
    }

    private class TimeReceive extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            assert action != null;
            if (action.equals(Intent.ACTION_TIME_TICK)) {
                tvWeek.setText(Objects.requireNonNull(TimeUtil.getTime()).get(2));
                tvDate.setText(TimeUtil.getTime().get(0));
                tvTime.setText(TimeUtil.getTime().get(1));
            }
        }
    }

    private class PagerAdapter extends FragmentPagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

}
